package guru.springframework;


import com.mongodb.MongoClientSettings;
import com.mongodb.reactivestreams.client.MongoClient;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.*;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;

import java.util.stream.Collectors;

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
        MongoReactiveAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class})
@EnableReactiveMongoRepositories
@AutoConfigureAfter(EmbeddedMongoAutoConfiguration.class)
class ApplicationConfiguration extends AbstractReactiveMongoConfiguration {


    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(ApplicationConfiguration.class);
        application.setWebApplicationType(WebApplicationType.NONE);
        application.run(args);
    }

    private final MongoClientSettings settings;

    public ApplicationConfiguration(ObjectProvider<MongoClientSettings> settings) {
        this.settings = settings.getIfAvailable();
    }


//    @Override
//    @Bean
//    @DependsOn("embeddedMongoServer")
//    public MongoClient mongoClient() {
//        int port = environment.getProperty("local.mongo.port", Integer.class);
//        return MongoClients.create(String.format("mongodb://localhost:%d", port));
//    }

    @Override
    protected String getDatabaseName() {
        return "reactive-mongo";
    }

    @Override
    public MongoClient reactiveMongoClient() {
        return null;
    }

    @Bean
    @ConditionalOnMissingBean
    public MongoClient reactiveStreamsMongoClient(MongoProperties properties,
                                                  Environment environment,
                                                  ObjectProvider<MongoClientSettingsBuilderCustomizer> builderCustomizers) {
        ReactiveMongoClientFactory factory = new ReactiveMongoClientFactory(properties,
                environment,
                builderCustomizers.orderedStream().collect(Collectors.toList()));
        MongoClient client = factory.createMongoClient(this.settings);
        return client;
    }
}
